package com.leave.dao;
import com.leave.entity.Instructor;
import com.leave.tools.MD5Util;
import org.apache.commons.lang3.StringUtils;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class InstructorDao {
    private JdbcTemplate jdbcTemplate;
    public List<Instructor> query(Instructor instructor){
        String sql ="select * from sys_instructor where 1=1 ";
        List<Object> paramValues=new ArrayList<>();
        List<Instructor> instructortList=new ArrayList<>();
        if(instructor!=null){
            if(instructor.getInstID()!=null&&!"".equals(StringUtils.trim(instructor.getInstID()))){
                sql+="and instID=?";
                paramValues.add(instructor.getInstID());
            }
        }
        instructortList =jdbcTemplate.query(sql, paramValues.toArray(), new RowMapper<Instructor>() {
            @Override
            public Instructor mapRow(ResultSet rs, int rowNum) throws SQLException {
                Instructor instructor1=new Instructor();
                String instID =rs.getString("instID");
                String instName =rs.getString("instName");
                String depID =rs.getString("depID");
                String password =rs.getString("password");
                String telephone = rs.getString("telephone");
                instructor1.setInstID(instID);
                instructor1.setDepID(depID);
                instructor1.setPassword(password);
                instructor1.setInstName(instName);
                instructor1.setTelephone(telephone);
                return instructor1;
            }
        });
        return instructortList;
    }

    public List<Map<String, Object>> selectList(String keyword) throws SQLException {
        String sql = "SELECT * FROM sys_instructor WHERE instID LIKE ? OR instName LIKE ? OR depID LIKE ? OR telephone LIKE ?";
        String likeKeyword = "%" + keyword + "%";
        String[] paramValues = {likeKeyword, likeKeyword,likeKeyword, likeKeyword};
        RowMapper<Map<String, Object>> rowMapper = new RowMapper<Map<String, Object>>() {
            @Override
            public Map<String, Object> mapRow(ResultSet rs, int rowNum) throws SQLException {
                Map<String, Object> map = new HashMap<>();
                int columnCount = rs.getMetaData().getColumnCount();
                for (int i = 1; i <= columnCount; i++) {
                    String columnName = rs.getMetaData().getColumnName(i);
                    Object columnValue = rs.getObject(i);
                    map.put(columnName, columnValue);
                }
                return map;
            }
        };
        return jdbcTemplate.query(sql, paramValues, rowMapper);
    }

    public void delete(String instID){
        String sql ="delete from sys_instructor where instID=?";
        String[]paramValues={instID};
        jdbcTemplate.update(sql,paramValues);
    }
    public void insert(Instructor instructor) {
        String password ="123";
        //声明插入SQL字符串变量sql
        String sql = "insert into sys_instructor(instID,instName,depID,password,telephone)values(?,?,?,?,?)";
        if (instructor != null) {
            String[] paramValues = {instructor.getInstID(), instructor.getInstName(),instructor.getDepID(),password,instructor.getTelephone()};
            jdbcTemplate.update(sql, paramValues);
        }
    }
    public void update(Instructor instructor){
        //声明插入SQL字符串变量sql
        String sql = "update sys_instructor set instName=?,depID=?,telephone=? where instID=?";
        if (instructor != null) {
            String[] paramValues = {instructor.getInstName(),instructor.getDepID(),instructor.getTelephone(),instructor.getInstID()};
            jdbcTemplate.update(sql, paramValues);
        }
    }
    /**
     *  setter注入方式，注意不需要定义getter方法。
     * @param jdbcTemplate
     */
    public void setJdbcTemplate(JdbcTemplate jdbcTemplate) {
        this.jdbcTemplate = jdbcTemplate;
    }
}
